package com.dzl.card;

import com.alibaba.fastjson.JSONObject;
import com.dzl.base.controller.BaseController;
import com.dzl.card.pojo.WelfareCard;
import com.dzl.card.pojo.query.WelfareCardQuery;
import com.dzl.card.pojo.vo.WelfareCardVo;
import com.dzl.card.service.WelfareCardService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 福利卡管理(WelfareCard)表控制层
 * @author caobucao
 * @since 2021-05-19 09:57:33
 */
@RestController
@RequestMapping("welfareCard")
public class WelfareCardController extends BaseController {

    @Autowired
    private WelfareCardService service;

    /**
     * 分页查询福利卡列表
     * @date 2021/5/19 11:06
     * @author caobucao
     * @param query 查询条件
     * @return com.dzl.util.PageUtils<com.dzl.card.entity.vo.WelfareCardVo>
     */
    @GetMapping("queryCardVoListPage")
    public PageUtils<WelfareCardVo> queryCardVoListPage(WelfareCardQuery query) {
        PageInfo<WelfareCardVo> pageInfo = service.selectCardVoListPage(query);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 保存福利卡
     * @date 2021/5/19 11:38
     * @author caobucao
     * @param welfareCard 福利卡对象
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("saveWelfareCard")
    public JsonResult<String> saveWelfareCard(@RequestBody WelfareCard welfareCard) {
        if(EmptyUtils.isEmpty(welfareCard.getName())) {
            return JsonResult.paramErrWithMsg("请输入福利卡名称");
        }
        if(EmptyUtils.isEmpty(welfareCard.getCardTypeId())) {
            return JsonResult.paramErrWithMsg("请选择福利卡类型");
        }
        if(EmptyUtils.isEmpty(welfareCard.getQuantity())) {
            return JsonResult.paramErrWithMsg("请输入卡数量");
        }else{
            if(welfareCard.getQuantity()>1000){
                return JsonResult.paramErrWithMsg("卡密数量限制1000张");
            }
        }
        if(EmptyUtils.isEmpty(welfareCard.getId())) {
            welfareCard.setUpdater(getAdminInfo().getId());
            welfareCard.setUpdaterName(getAdminInfo().getUserName());
            welfareCard.setCreater(getAdminInfo().getId());
            welfareCard.setCreaterName(getAdminInfo().getUserName());
            welfareCard.setEnterpriseId(0);
        } else {
            return JsonResult.paramErrWithMsg("不允许修改福利卡");
        }
        Integer count = service.saveWelfareCard(welfareCard);
        return count > 0 ? JsonResult.success("新增成功，共新增："+count+"条卡密") : JsonResult.paramErrWithMsg("保存失败！");
    }

    /**
     * 修改福利卡封面
     * @date 2021/7/20 10:49
     * @author caobucao
     * @param jsonObject 入参，id及封面
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("updateCardImg")
    public JsonResult<Integer> updateCardImg(@RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        String img = jsonObject.getString("img");
        if(EmptyUtils.isEmpty(id)) {
            return JsonResult.paramErrWithMsg("请传入id");
        }
        if(EmptyUtils.isEmpty(img)) {
            return JsonResult.paramErrWithMsg("未获取到上传的封面");
        }
        WelfareCard welfareCard = new WelfareCard();
        welfareCard.setId(id);
        welfareCard.setImg(img);
        welfareCard.setUpdateTime(new Date());
        welfareCard.setUpdater(getAdminInfo().getId());
        welfareCard.setUpdaterName(getAdminInfo().getUserName());
        return JsonResult.success(service.updateByPrimaryKeySelective(welfareCard));
    }

    /**
     * 修改福利卡备注
     * @date 2021/7/20 10:47
     * @author caobucao
     * @param jsonObject 入参 id及备注
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("/updateCardRemark")
    public JsonResult<Integer> updateCardRemark(@RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        String remark = jsonObject.getString("remark");
        if(EmptyUtils.isEmpty(id)) {
            return JsonResult.paramErrWithMsg("请传入id");
        }
        WelfareCard welfareCard = new WelfareCard();
        welfareCard.setId(id);
        welfareCard.setRemark(remark);
        welfareCard.setUpdateTime(new Date());
        welfareCard.setUpdater(getAdminInfo().getId());
        welfareCard.setUpdaterName(getAdminInfo().getUserName());
        return JsonResult.success(service.updateByPrimaryKeySelective(welfareCard));
    }

    /**
     * 修改福利卡名称
     * @date 2021/7/31 9:20
     * @author caobucao
     * @param jsonObject 入参 id及名字
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("/updateCardName")
    public JsonResult<Integer> updateCardName(@RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        String cardName = jsonObject.getString("cardName");
        if(EmptyUtils.isEmpty(id)) {
            return JsonResult.paramErrWithMsg("请传入id");
        }
        if(EmptyUtils.isEmpty(cardName)) {
            return JsonResult.paramErrWithMsg("请填写卡名称！");
        }
        WelfareCard welfareCard = new WelfareCard();
        welfareCard.setId(id);
        welfareCard.setName(cardName);
        welfareCard.setUpdateTime(new Date());
        welfareCard.setUpdater(getAdminInfo().getId());
        welfareCard.setUpdaterName(getAdminInfo().getUserName());
        return JsonResult.success(service.updateCardName(welfareCard));
    }

}
